package com.xgit.iov.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xgit.iov.common.core.entity.IovUser;
import com.xgit.iov.common.core.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @Author: william
 * @Date: 2020/6/5 14:24
 * mybatisplus自定义填充公共字段
 */

public class MybatisMetaObjectHandler implements MetaObjectHandler {

	private static String[] keyNames = {"delFlag","createTime","updateTime","createBy","updateBy"};
	//新增填充
	@Override
	public void insertFill(MetaObject metaObject) {
		setFieldValByName(keyNames[0], "0", metaObject);
		setFieldValByName(keyNames[1], LocalDateTime.now(), metaObject);
		setFieldValByName(keyNames[2], LocalDateTime.now(), metaObject);

		IovUser user = SecurityUtils.getUser();
		if (user != null) {
			setFieldValByName(keyNames[3],user.getId(), metaObject);
			setFieldValByName(keyNames[4],user.getId(), metaObject);
		}
	}

	//更新填充
	@Override
	public void updateFill(MetaObject metaObject) {
		setFieldValByName(keyNames[2],LocalDateTime.now(), metaObject);
		IovUser user = SecurityUtils.getUser();
		if (user != null) {
			setFieldValByName(keyNames[4],user.getId(),metaObject);
		}
	}
}
